//
//  Sky72ViewController.m
//  Sky72
//
//  Created by ladaka31 ladaka on 13. 9. 16..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "Sky72ViewController.h"
#import "Sky72SlideMainViewController.h"
#import "Sky72CourseMainViewController.h"
#import "Sky72ReservationViewController.h"
#import "PPRevealSideViewController.h"
#import "Sky72LoginViewController.h"
#import "Sky72HandicapMainViewController.h"
#import "Sky72Facebook.h"
#import "Sky72MoreMainViewController.h"
#import "Sky72LikeCaddieViewController.h"

@interface Sky72ViewController ()

@end

@implementation Sky72ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    navBar = [[NaviBarUtil alloc] initWithNavigationController:self.navigationController navigationItem:self.navigationItem];
    [navBar naviBarSetting:@"SKY72 GOLF&RESORT"];
    [navBar addLeftButtonNormalImage:[UIImage imageNamed:@"head_btn_close.png"]
                      highlightImage:[UIImage imageNamed:@"head_btn_close_ov.png"]
                              target:self
                              action:@selector(onClickMenu)];
    [navBar addRightButtonNormalImage:[UIImage imageNamed:@"head_btn_complete.png"]
                       highlightImage:[UIImage imageNamed:@"head_btn_complete_ov.png"]
                               target:self
                               action:@selector(onClickWeather)];
    
    // Scroll view
    for (int i=0; i<6; i++) {
        CGRect frame = CGRectMake(svScrollView.frame.size.width * i,
                                  0,
                                  svScrollView.frame.size.width,
                                  svScrollView.frame.size.height);
        UILabel *label = [[UILabel alloc] initWithFrame:frame];
        label.textAlignment = UITextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:144.0];
        label.text = [NSString stringWithFormat:@"%d", i];
        
        [svScrollView addSubview:label];
    }
    
    svScrollView.contentSize = CGSizeMake(svScrollView.frame.size.width * 6, svScrollView.frame.size.height);
    
    svScrollView.delegate = self;
    
    // Pager
    [pagerView setImage:[UIImage imageNamed:@"a"]
       highlightedImage:[UIImage imageNamed:@"a-h"]
                 forKey:@"a"];
    [pagerView setImage:[UIImage imageNamed:@"b"]
       highlightedImage:[UIImage imageNamed:@"b-h"]
                 forKey:@"b"];
    [pagerView setImage:[UIImage imageNamed:@"c"]
       highlightedImage:[UIImage imageNamed:@"c-h"]
                 forKey:@"c"];
    
    [pagerView setPattern:@"abc"];
    
    pagerView.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(preloadLeft) object:nil];
    [self performSelector:@selector(preloadLeft) withObject:nil afterDelay:0.3];
}

#pragma mark - click event
- (IBAction)onClickCourse:(id)sender {
    Sky72CourseMainViewController *controller = [[Sky72CourseMainViewController alloc]
                                             initWithNibName:@"Sky72CourseMainViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)onClickQuickReservation:(id)sender {
}

- (IBAction)onClickReservation:(id)sender {
//    Sky72ReservationViewController *controller = [[Sky72ReservationViewController alloc]
//                                             initWithNibName:@"Sky72ReservationViewController" bundle:nil];
//    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)onClickMore:(id)sender {
    Sky72MoreMainViewController *controller = [[Sky72MoreMainViewController alloc]
                                                  initWithNibName:@"Sky72MoreMainViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)onClickReward:(id)sender {
}

- (IBAction)onClickLogin:(id)sender {
    Sky72LoginViewController *controller = [[Sky72LoginViewController alloc]
                                                  initWithNibName:@"Sky72LoginViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)onClickLanking:(id)sender {
    Sky72HandicapMainViewController *controller = [[Sky72HandicapMainViewController alloc]
                                            initWithNibName:@"Sky72HandicapMainViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)onClickCaddieLike:(id)sender {
        Sky72LikeCaddieViewController *controller = [[Sky72LikeCaddieViewController alloc]
                                                       initWithNibName:@"Sky72LikeCaddieViewController" bundle:nil];
        [self.navigationController pushViewController:controller animated:YES];
}

//- (IBAction)onClickFb:(id)sender {
//    Sky72Facebook *controller = [[Sky72Facebook alloc]
//                                                   initWithNibName:@"Sky72Facebook" bundle:nil];
//    [self.navigationController pushViewController:controller animated:YES];
//}

- (void)onClickMenu {
    Sky72SlideMainViewController *controller = [[Sky72SlideMainViewController alloc]
                                                initWithNibName:@"Sky72SlideMainViewController" bundle:nil];

    
    self.revealSideViewController.delegate = self;
    
    // facebook과 같은 방식으로 push
    [self.revealSideViewController pushViewController:controller onDirection:PPRevealSideDirectionLeft animated:YES];
    //    [self.navigationController pushViewController:controller animated:YES];
}

- (void)onCLickWeather {
    Sky72SlideMainViewController *controller = [[Sky72SlideMainViewController alloc]
                                                initWithNibName:@"Sky72SlideMainViewController" bundle:nil];
    
    self.revealSideViewController.delegate = self;
    
    // facebook과 같은 방식으로 push
    [self.revealSideViewController pushViewController:controller onDirection:PPRevealSideDirectionLeft animated:YES];
    //    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark - PPRevealSideViewController function
//- (NSArray *)customViewsToAddPanGestureOnPPRevealSideViewController:(PPRevealSideViewController *)controller {
//    NSLog(@"customViewsToAddPanGestureOnPPRevealSideViewController");
//    NSArray *array = [NSArray arrayWithObject:self.view];
//    return array;
//}

- (void)preloadLeft {
    Sky72SlideMainViewController *controller = [[Sky72SlideMainViewController alloc]
                                                initWithNibName:@"Sky72SlideMainViewController" bundle:nil];
    
    self.revealSideViewController.delegate = self;
    
    [self.revealSideViewController preloadViewController:controller
                                                 forSide:PPRevealSideDirectionLeft
                                              withOffset:70];
}

- (void)viewDidUnload {
    svScrollView = nil;
    pagerView = nil;
    [super viewDidUnload];
}

#pragma mark - UIScrollViewDelegate, MCPagerViewDelegate
- (void)updatePager
{
    pagerView.page = floorf(svScrollView.contentOffset.x / svScrollView.frame.size.width);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self updatePager];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) {
        [self updatePager];
    }
}

- (void)pageView:(MCPagerView *)pageView didUpdateToPage:(NSInteger)newPage
{
    CGPoint offset = CGPointMake(svScrollView.frame.size.width * pagerView.page, 0);
    [svScrollView setContentOffset:offset animated:YES];
}

@end
